#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

//线程分离
void* MyThreadStrat(void* arg)
{
    (void)arg;
    int err_code = pthread_detach(pthread_self());
    printf(" err_code:%d\n",err_code);
    int i = 0;
    while(i < 10)
    {
        i++;
        printf("i am MyThreadStrat\n");
        pthread_cancel(pthread_self());
        sleep(1);
    }
    sleep(5);
    return NULL;
}

int main()
{
    pthread_t tid;
    void* ref;
    int ret = pthread_create(&tid, NULL, MyThreadStrat, NULL);
    pthread_join(tid,&ref);
    printf("----------ret------:%d\n",ret);
    if(ret != 0)
    {
        perror("pthread_create");
        return 0;
    }

    while(1)
    {
        printf("i am main thread\n");
        sleep(2);
    }
    return 0;
}